//
//  GroupBookingrTableViewCell.m
//  quxiangtao
//
//  Created by E人一铺 on 2019/2/27.
//  Copyright © 2019年 shanhaijin. All rights reserved.
//

#import "GroupBookingrTableViewCell.h"
#import "BargainTableViewModel.h"

@interface GroupBookingrTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *goodsImage;
@property (weak, nonatomic) IBOutlet UILabel *goodsName;
@property (weak, nonatomic) IBOutlet UILabel *goodsCount;
@property (weak, nonatomic) IBOutlet UILabel *goodsPrice;
@property (weak, nonatomic) IBOutlet UILabel *goodsRenshu;
@property (weak, nonatomic) IBOutlet UILabel *goodsAllNum;
@property (weak, nonatomic) IBOutlet UILabel *daojishi;

// 创建活动定时器
@property (nonatomic, strong) NSTimer *activeTimer;

@end


@implementation GroupBookingrTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    _activeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(activeCountDownAction) userInfo:nil repeats:YES];
    
   
}

- (void)setModel:(BargainTableViewModel *)model {
    
    _model = model;
    NSLog(@"%@",_model.leave_time);
    
    NSString *picStr = @"";
    if ([model.goods_logo hasPrefix:@"http://"] || [model.goods_logo hasPrefix:@"https://"]) {
        picStr = model.goods_logo;
    }else {
        picStr = [NSString stringWithFormat:@"http://%@", model.goods_logo];
    }
    [self.goodsImage sd_setImageWithURL:[NSURL URLWithString:picStr]];
    self.goodsName.text = [NSString stringWithFormat:@"%@", model.goods_name];

//    self.goodsName.text = [NSString stringWithFormat:@"%@ %@ %@", model.goods_name, model.goods_specification, model.num];
    
    self.goodsCount.hidden = YES;
    self.goodsPrice.attributedText = [self deleteLine: [NSString stringWithFormat:@"市场价：¥%@", model.goods_price]];
    
    self.goodsAllNum.text = [NSString stringWithFormat:@"¥%@/%@人成团", model.goods_favourable_price, model.member_num];
    NSString *string = [NSString stringWithFormat:@"¥%@/%@人成团", model.goods_favourable_price, model.member_num];
    
    self.goodsAllNum.attributedText = [self AttributedString:string];

    
    
    if ([model.status intValue] == 1) {// 拼团中
        self.goodsRenshu.text = [NSString stringWithFormat:@"拼团中：已拼%ld人", (long)model.now_num];
    }
    else if ([model.status intValue] == 2){
        self.goodsRenshu.text = [NSString stringWithFormat:@"拼团成功：已拼%ld人",(long)model.now_num];
    }else if ([model.status intValue] == 3){
        self.goodsRenshu.text = [NSString stringWithFormat:@"拼团失败：已拼%ld人",(long)model.now_num];
    }

     [_activeTimer fire];
}

// 实现活动倒计时
-(void)activeCountDownAction {
    
    // 1.计算截止时间与当前时间差值
    // 倒计时的时间 测试数据
    NSString *deadlineStr = _model.expire_time;

    // 当前时间的时间戳
    NSDate *dateNow = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *nowStr = [formatter stringFromDate:dateNow];
    // 计算时间差值
    NSInteger secondsCountDown = [self getDateDifferenceWithNowDateStr:nowStr deadlineStr:deadlineStr];
    
    // 2.先递减 倒计时-1(总时间以秒来计算)
    secondsCountDown--;
    
    // 3.给时分秒字符串通过递减过后的秒数,重新计算数值,并输出显示
    // 重新计算 时/分/秒
    NSString *str_hour = [NSString stringWithFormat:@"%02ld", secondsCountDown / 3600];
    NSString *str_minute = [NSString stringWithFormat:@"%02ld", (secondsCountDown % 3600) / 60];
    NSString *str_second = [NSString stringWithFormat:@"%02ld", secondsCountDown % 60];
    NSString *format_time = [NSString stringWithFormat:@"%@ : %@ : %@", str_hour, str_minute, str_second];
    // 修改倒计时标签及显示内容
    self.daojishi.text = [NSString stringWithFormat:@"剩余时间： %@", format_time];
    
    // 当倒计时结束时做需要的操作: 比如活动到期不能提交
    if(secondsCountDown <= 0) {
        
        self.daojishi.text = @"当前活动已结束";
        
        [_activeTimer invalidate];
        _activeTimer = nil;
        
        return;
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

/**
 *  获取当天的字符串
 *  @return 格式为年-月-日 时分秒
 */
- (NSString *)getCurrentTimeyyyymmdd {
    
    NSDate *now = [NSDate date];
    NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
    formatDay.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *dayStr = [formatDay stringFromDate:now];
    
    return dayStr;
}

/**
 *  获取时间差值  截止时间-当前时间
 *  nowDateStr : 当前时间
 *  deadlineStr : 截止时间
 *  @return 时间戳差值
 */
- (NSInteger)getDateDifferenceWithNowDateStr:(NSString*)nowDateStr deadlineStr:(NSString*)deadlineStr {
    
    NSInteger timeDifference = 0;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yy-MM-dd HH:mm:ss"];
    NSDate *nowDate = [formatter dateFromString:nowDateStr];
    NSDate *deadline = [formatter dateFromString:deadlineStr];
    NSTimeInterval oldTime = [nowDate timeIntervalSince1970];
    NSTimeInterval newTime = [deadline timeIntervalSince1970];
    timeDifference = newTime - oldTime;
    
    return timeDifference;
}

- (NSMutableAttributedString *)AttributedString:(NSString *)string
{
    NSArray *arr = [string componentsSeparatedByString:@"/"];
    
    // 创建Attributed
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@/%@",arr.firstObject,arr.lastObject]];
    
    //第一部分修改
    // 需要改变的第一个文字的位置
    NSUInteger firstLoc = 0;
    // 需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:@"/"].location;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);

    // 改变字体大小及类型
    [noteStr addAttribute:NSFontAttributeName value:Font(32) range:range];
    
    
    //第二部分修改
    // 需要改变的第一个文字的位置
    NSUInteger firstLoc1 = [[noteStr string] rangeOfString:@"/"].location + 1;
    // 需要改变的最后一个文字的位置
    NSUInteger secondLoc1 = [noteStr string].length;
    // 需要改变的区间
    NSRange range1 = NSMakeRange(firstLoc1, secondLoc1 - firstLoc1);

    // 改变字体大小及类型
    [noteStr addAttribute:NSFontAttributeName value:Font(26) range:range1];

    return noteStr;
    
    
}
- (NSMutableAttributedString *)deleteLine:(NSString *)string {
    
    
    NSArray *arr = [string componentsSeparatedByString:@"："];
    
    // 创建Attributed
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@：%@",arr.firstObject,arr.lastObject]];
    
    
    // 需要改变的第一个文字的位置
    NSUInteger firstLoc1 = [[attri string] rangeOfString:@"："].location + 1;
    // 需要改变的最后一个文字的位置
    NSUInteger secondLoc1 = [attri string].length;
    // 需要改变的区间
    NSRange range1 = NSMakeRange(firstLoc1, secondLoc1 - firstLoc1);


    
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:range1];
//    [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:range1];
    

    return attri;
    
}

@end
